//
//  CancellationVC.m
//  HuoFeiDaShipper
//
//  Created by mac on 2020/1/10.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "CancellationVC.h"
#import "CancellationByCodeVC.h"
#import "CJWKWebViewVC.h"
#import "CJAppInfo.h"
@interface CancellationVC ()
@property (weak, nonatomic) IBOutlet UIView *viewHeaderBg;
@property (weak, nonatomic) IBOutlet UILabel *labNameTitle;
@property (weak, nonatomic) IBOutlet UILabel *labContent;
@property (strong, nonatomic) CJButton *btnLogOut;
@property (nonatomic,strong) NSArray *arrList;
@property (nonatomic,strong) CJView *viewBottom;
@property (nonatomic,strong) CJButton *btnAgree;
@property (nonatomic,strong) CJButton *btnUserProtocol;
@end

@implementation CancellationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"注销账号")];
    [self setBackLeftButton:@""];
    self.labNameTitle.textColor = [UIColor cjMainTextColor];
    self.labContent.textColor = [UIColor cjSubheadTextColor];
    self.arrList = [[NSArray alloc]init];
    [self loadData];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.isCancellFail) {
        self.labNameTitle.text = @"注销失败";
        self.labContent.text = @"由于以下原因，账号无法注销";
    }else{

        self.labNameTitle.text = CJStringWithFormat(@"注销%@所绑定的账号",self.strPhoneNum);
        self.labContent.text = @"注销账号后信息将被清空无法找回！注销前请确认以 下操作已经完成。";
    }
}

- (void)loadData{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/party/bye",CJBaseUrl);
//    NSDictionary *dicPar = @{@"page":CJStringWithFormat(@"%d",self.page),
//                             @"pageSize":@"10",
//    };
    NSDictionary *dic = [[NSDictionary alloc]init];

    @weakify(self);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dic success:^(id  _Nonnull responseObject) {
        
//        DLog(@"++++++++%@",responseObject);
        @strongify(self);
        [self createUIWithData:(NSArray *)responseObject];
        self.arrList = (NSArray *)responseObject;
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}


- (void)createUIWithData:(NSArray *)list{
    
//    NSArray *list = @[@"① 没有在途运单",@"② 运单全部结算完毕",@"③ 运单全部结算完毕"];
    for (int i = 0; i < list.count; i++) {
        
        NSDictionary *dict = list[i];
        
        UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.viewHeaderBg.bottom + i * LW(44), SCREEN_WIDTH, LW(44))];
        liebiaoView.backgroundColor = [UIColor cjWhiteColor];
        [self.view addSubview:liebiaoView];

        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, LW(5), kScreenWidth - 2*CJToLeftMargin, LW(34))];
        label.text = CJStringWithFormat(@"  %d %@",i+1 ,dict[@"name"]);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont cjTitleFont15];
       
        [liebiaoView addSubview:label];
        
        if ([CJStringWithFormat(@"%@",dict[@"result"]) isEqualToString:@"YES"]) {
            label.textColor =[UIColor cjMainColor];;
        }else{
            label.textColor =CJRGB16Color(0xFF7E15);
        }
        
        UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, liebiaoView.height -1, SCREEN_WIDTH, 1)];
        bg.backgroundColor = [UIColor cjBackgroundColor];
        [liebiaoView addSubview:bg];
        
    }
    
    if (!_viewBottom) {
        _viewBottom = CJView.viewInit
        .viewbgColor([UIColor cjBackgroundColor])
        ;
    }
    _viewBottom.frame = CGRectMake(0, self.viewHeaderBg.bottom + (list.count + 1) * LW(44), SCREEN_WIDTH, LW(80));
    [self.view addSubview:self.viewBottom];
    [self.viewBottom addSubview:self.btnAgree];
    [self.viewBottom addSubview:self.btnUserProtocol];
    [self.viewBottom addSubview:self.btnLogOut];
    
//    self.btnAgree.hidden = YES;
//    self.btnUserProtocol.hidden = YES;

}
- (void)btnAgreeAction:(CJButton *)sender{
    
    sender.selected = !sender.selected;
    
//    .btnNormalImgName(@"unSelected")
//          .btnSelectImgName(@"selected")
    

//    if (self.btnAgree.selected) {
////        self.btnAgree.selected = NO;
//        [self.btnAgree setImage:[UIImage imageNamed:@"unSelected"] forState:UIControlStateNormal];
//
////    }else{
////        self.btnAgree.selected = YES;
//        [self.btnAgree setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];

//    }
}
/// 协议
- (void)btnUserProtocolAction{
    
    //    0716 release0716-agreement-newhtml 修改协议地址传参agreementType：协议类型:1=司机端隐私协议/2=司机端用户协议/3=司机端注销协议/4=司机端物流协议/5=接单协议/6=司机端SDK协议/7=货主端隐私协议/8=货主端用户协议/9=货主端注销协议/10=货主端物流协议/11=货主端SDK协议/12=PC端隐私协议/13=PC端用户协议/14=PC端物流协议/15=网商协议  例如 /agreement/agreement.html?agreementType=1
    
    CJWKWebViewVC *vc = [CJWKWebViewVC new];
    vc.strTitle = @"注销协议";
    vc.useUTF8 = YES;
    vc.strUrl = CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=9",CJBaseUrl);
    [self pushVc:vc];
    
}
- (void)btnLogOutAction:(CJButton *)sender{
    
    DLog(@"=====%@",self.arrList);
    
    if (self.btnAgree.selected == NO) {
        [CJAlert showStringDismissWithDelay:@"请先阅读并同意注销协议"];
        return ;
    }
    
    int y = 0;
    for (int i = 0; i < self.arrList.count; i++) {

        NSDictionary *dict = self.arrList[i];
        if ([CJStringWithFormat(@"%@",dict[@"result"]) isEqualToString:@"YES"]) {
        }else{
            y++;
        }
    }

    if (y == 0) {
        CancellationByCodeVC *vc = [CancellationByCodeVC new];
        vc.strPhoneNum = self.strPhoneNum;
        [self pushVc:vc];
    }else{
        [CJAlert showStringDismissWithDelay:@"您不满足注销条件"];
    }

}
    
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (CJButton *)btnAgree{
    if (!_btnAgree) {
        _btnAgree = CJButton.btnInit
        .btnFrame(CGRectMake(20, 0, 70, 30))
        .btnImgAlignment(CJImageAlignmentLeft)
        .btnText(@"同意")
        .btnNormalImgName(@"zcj_unSelected")
        .btnSelectImgName(@"selected")
        .btnNormalTitleColor([UIColor cjMainTextColor])
    .btnTargetAction(self,@selector(btnAgreeAction:))
        .btnSpaceBetweenTitleAndImage(10)
        ;
    }
    return _btnAgree;
}

- (CJButton *)btnUserProtocol{
    if (!_btnUserProtocol) {
        _btnUserProtocol = CJButton.btnInit
        .btnFrame(CGRectMake(_btnAgree.right, _btnAgree.top , LW(280), _btnAgree.height))
        .btnImgAlignment(CJImageAlignmentLeft)
        .btnText(CJStringWithFormat(@"《%@账户注销须知》",[CJAppInfo appName]))
//        .btnNormalImgName(@"unSelected")
//        .btnSelectImgName(@"selected")
        .btnNormalTitleColor([UIColor cjMainColor])
        .btnTargetAction(self,@selector(btnUserProtocolAction))
        .btnTitleHorizontalAlignment(UIControlContentHorizontalAlignmentLeft)
//        .btnSpaceBetweenTitleAndImage(10)
        ;
    }
    return _btnUserProtocol;
}
- (CJButton *)btnLogOut{
    if (!_btnLogOut) {
        _btnLogOut = CJButton.btnInit
        .btnFrame(CGRectMake(20, 36, kScreenWidth - 40, LW(44)))
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnFontSize(17.0)
        .btnText(CJLString(@"申请注销"))
        .btnTargetAction(self,@selector(btnLogOutAction:))
        .btnCornerRadius(5)
        ;
    }
    return _btnLogOut;
}
@end
